/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SendProcess.java
 *
 * Created on Oct 17, 2011, 9:56:03 AM
 */
package azhar.view.com;

import azhar.com.Messages;
import azhar.com.Separate;
import azhar.view.Main;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Al-Insan
 */
public class SendProcess extends javax.swing.JFrame implements Runnable {

    Main main;
    String number, tex;
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    Thread th = new Thread(this);
    int i = 0;
    ArrayList<String> numbers;
    ArrayList<Messages> messages;

    /** Creates new form SendProcess */
    public SendProcess(Main m, boolean modal) {
        main = m;
        initComponents();
        setLocation((int) (screen.getWidth() - getWidth()), screen.height - (getHeight() + 45));
    }

    public void addMessage(ArrayList<Messages> msg) {
        messages = new ArrayList<Messages>();
        for (int i = 0; i < msg.size(); i++) {
            numbers = new Separate().getWord(msg.get(i).getNumber());
            System.out.println(numbers.size());
            if (numbers.size() > 1) {
//                System.out.println(numbers.size());
                for (int j = 0; j < numbers.size(); j++) {
                    messages.add(new Messages(numbers.get(j), msg.get(i).getText()));
                }
            } else {
                messages.add(new Messages(numbers.get(0), msg.get(i).getText()));
            }
        }
        pg.setMaximum(messages.size() + 1);
        setVisible(true);
        th.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        pg = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setAlwaysOnTop(true);
        setUndecorated(true);
        getContentPane().setLayout(new java.awt.CardLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setLayout(new java.awt.GridLayout());

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/azhar/view/images/delete_up.png"))); // NOI18N
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel1MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel1MouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jLabel1MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel1MouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pg, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.add(jPanel1);

        getContentPane().add(jPanel2, "card2");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseEntered
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/azhar/view/images/delete_over.png")));
    }//GEN-LAST:event_jLabel1MouseEntered

    private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseExited
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/azhar/view/images/delete_up.png")));
    }//GEN-LAST:event_jLabel1MouseExited

    private void jLabel1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MousePressed
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/azhar/view/images/delete_down.png")));
    }//GEN-LAST:event_jLabel1MousePressed

    private void jLabel1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseReleased
        th.stop();
        for (int j = i; j < numbers.size(); j++) {
            main.save(messages.get(j).getNumber(), messages.get(j).getText());
        }
        main.setProcess(0);
        main.setEnabled(true);
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jLabel1MouseReleased
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JProgressBar pg;
    // End of variables declaration//GEN-END:variables

    public void run() {
        main.setProcess(1);
        int a = 1;
        for (i = 0; i < messages.size(); i++) {
            pg.setValue(i + 1);
            if (i == (a * 15)) {
                try {
                    th.sleep(6000);
                    a++;
                } catch (InterruptedException ex) {
                }
            }
            try {
                main.sendingMessage(messages.get(i).getNumber(), messages.get(i).getText());
            } catch (Exception ex) {
                Logger.getLogger(SendProcess.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        pg.setValue(pg.getMaximum());
        try {
            th.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(SendProcess.class.getName()).log(Level.SEVERE, null, ex);
        }
        main.setProcess(0);
        main.setEnabled(true);
        setVisible(false);
        th.stop();
        dispose();
    }
}
